from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.spus import SPUSerializer, BrandSerializer, CategorySerializer
from meiduo_admin.utils import PageNum


class SPUView(ModelViewSet):
    # 指定查询集
    queryset = SPU.objects.all()
    # 指定序列化器
    serializer_class = SPUSerializer
    # 指定分页器
    pagination_class = PageNum
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    # 获取品牌信息
    def simple(self, request):
        brands = Brand.objects.all()
        ser = BrandSerializer(brands, many=True)
        return Response(ser.data)
    # 获取一级分类
    def category(self, request):
        categories = GoodsCategory.objects.filter(parent=None)
        ser = CategorySerializer(categories, many=True)
        return Response(ser.data)
    # 获取二三级分类
    def category1(self, request, pk):
        categories = GoodsCategory.objects.filter(parent_id=pk)
        ser = CategorySerializer(categories, many=True)
        return Response(ser.data)
